<?php
/*
 * @Create By: Vscode
 * @Author: falcon
 * @Date: 2019-05-03 08:40:06
 */
use application\model\PdoModel;

function createGuid(){
    return  md5 ( uniqid ( mt_rand (), true ) );
}

// 判断SKU类型； 是成品还是配件
function getSKUtype($sku){
    if(empty($sku)) return null;
    $sql = "select id from product where sku='$sku' limit 1";
    $res = PdoModel::selectData($sql,[],'获取SKU类型');
    if(!empty($res[0]['id'])) return 1;

    $sql = "select id from product_acc where sku='$sku' limit 1";
    $res = PdoModel::selectData($sql,[],'获取SKU类型');
    if(!empty($res[0]['id'])) return 2;

    return 0;
}

    //只要经过order_info 结果集， 就需要过滤下   新增  20191004
function combine_multi_sku($data){
    $sku_uni_arr = [];
    $order_num_uni = [];

    foreach($data as $k=>$v){ //这里 的 sku  和 order_number 必须要去重
        $sku_uni_arr[$k]['sku'] = $v['sku'];
        $sku_uni_arr[$k]['order_number'] = $v['order_number'];//这个是有重复的 
        $order_num_uni[] = $v['order_number'];  //订单号去重
    }//foreach

    $sku = [];
    $order_num = [];
    foreach($sku_uni_arr as $k=>$v){
        $sku[] = $v['sku'];
        $order_num[] = $v['order_number'];
    }

    $sku = array_unique($sku);
    $order_num = array_unique($order_num);

    $sku = array_values($sku);
    $order_num = array_values($order_num);

    $folder = [];
    foreach($sku as $k=>&$v){
        $folder[$k]['sku'] = $v;
    }
    
    foreach($order_num as $k=>$v){
        $folder[$k]['order_number'] = $v;
    }

    //以上 获取到唯一的 SKU 和 order_num
    $data_f = $data;
    foreach($data_f as $k=>$v){		//这里循环 不能使用 & 地址符来运算，在tp3框架最后一个元素 会有问题，会把最后一个元素用倒数第二个元素替代， 所以最后两个会重复， 直接用 $xx[$k] = .... 不会有这问题

        $flag = 0;
        $location_index = [];//以数组的方式 记录键值

        foreach($data as $k1=>$v1){
            
            if(!empty($v1['sku']) && !empty($v['sku'])  && $v['sku'] == $v1['sku'] && $v['order_number'] == $v1['order_number']){
                $flag +=1;
                $location_index[] = $k1;
                if($flag>1){//全部累加到 第一个 SKU上
                    $data[$location_index[0]]['purchase_quantity'] += $v1['purchase_quantity'];
                    $data[$location_index[0]]['quantity'] += $v1['quantity'];
                    $data[$location_index[0]]['alsobuy'] += $v1['alsobuy'];
                    $data[$location_index[0]]['cumulative_quantity'] += $v1['cumulative_quantity'];
                    // var_dump('Dump Something....!');
                    unset($data[$k1]);
                }//if

            }//if
        }//foreach inside

    }//foreach


    $data = array_values($data);
    return $data;
}//func


/**
 * 递归遍历获取目录下的所有文件($allowFiles不为空时为指定类型)
 * @param string $path
 * @param stirng $allowFiles ("png|jpg|jpeg|gif|bmp")
 * @param array $files
 * @return array
 */
function getAllFiles($path = '', $allowFiles = '', &$files = [])
{
    if (!is_dir($path)) {
        return null;
    }
    if(substr($path, strlen($path) - 1) != '/') {//如果没有'/'结尾的，补上一个
        $path .= '/';
    }
    $handle = opendir($path);
    while (false !== ($file = readdir($handle))) {
        if ($file != '.' && $file != '..') {
            $path2 = $path . $file;
//            if (is_dir($path2)) {
//                getAllFiles($path2, $allowFiles, $files);
//            } else {
//                if (empty($allowFiles) || $allowFiles == '*') {
//                    $files[] = array('path'=>$path2,'mtime'=> filemtime($path2));//返回文件上次被修改的时间，出错时返回 FALSE，时间以 Unix 时间戳的方式返回
//                } else {
//                    if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
//                        $files[] = array('path'=>$path2,'mtime'=> filemtime($path2));
//                    }
//                }
//            }
            if (empty($allowFiles) || $allowFiles == '*') {
                $files[] = array('path'=>$path2,'mtime'=> filemtime($path2));//返回文件上次被修改的时间，出错时返回 FALSE，时间以 Unix 时间戳的方式返回
            } else {
                if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
                    $files[] = array('path'=>$path2,'mtime'=> filemtime($path2));
                }
            }
        }
    }
    return $files;
}